package com.team.easybuy.controller;

import com.team.easybuy.entity.UserAddress;
import com.team.easybuy.service.AddressService;
import com.team.easybuy.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;
    @Resource
    private UserService userService;

    @PostMapping("/addAddress")
    public String addAddress(@RequestBody UserAddress userAddress) {
        String rs = null;
        System.out.println(userAddress.toString());
        if (addressService.addUserAddress(userAddress)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @PostMapping("/editAddress")
    public String editAddress(@RequestBody UserAddress userAddress) {
        String rs = null;
        System.out.println("修改" + userAddress.toString());
        if (addressService.editUserAddress(userAddress)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/setDefault")
    public String setDefault(@RequestBody UserAddress userAddress) {
        String rs = null;
        if (addressService.setDefaultAddress(userAddress)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/delAddress")
    public String delAddress(@RequestParam Integer id) {
        String rs = null;
        if (addressService.deleteUserAddress(id)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/show")
    public List<UserAddress> showUserAddress(Integer userId) {
        System.out.println("userId" + userId);
        List<UserAddress> userAddressesList = addressService.getUserAddressList(userId);
        return userAddressesList;
    }

    @RequestMapping("/getAddress/{id}")
    public UserAddress getAddress(@PathVariable Integer id) {
        return addressService.getUserAddress(id);
    }
}
